// when the page gets shown the current level is loaded from local storage
$( document ).delegate("#levelpage", "pageshow", function() {
	var currentLevel = parseInt(localStorage.currentLevel);
	loadLevel(currentLevel);
});

// intervalID holds a reference to the update interval (box2Dweb)
// intervalID is set in the levelX.js files
var intervalID;
function stopUpdateFunction() {
	if(!(intervalID == null)) {
		window.clearInterval(intervalID); //intervalID is set in levelX.js where X is a number
		intervalID = null;
	}
}

//reloads the page
function reloadPage() {
	stopUpdateFunction();
	
	location.reload();
}

//reset the canvas to show other stuff
function resetCanvas() {
	stopUpdateFunction();
	
	var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');
    context.clearRect(0, 0, canvas.width, canvas.height);
}

//this gets called from the levelX.js files
function levelFinished() {
	//todo: save data
	$( "#popup" ).popup( "open" );
}

//open the next level and save new level index
function next() {
	$( "#popup" ).popup( "close" );
	var currentLevel = parseInt(localStorage.currentLevel);
	localStorage.currentLevel = "" + ++currentLevel;
	
	reloadPage();
	
	/*
	url = location.protocol + '//' + location.host + location.pathname;
	$.mobile.changePage(
		"#",
	    {
	      allowSamePageTransition : true,
	      transition              : 'slide',
	      showLoadMsg             : false,
	      reloadPage              : true
	    }
	); */ 
}

//open the menu
function menu() {
	stopUpdateFunction();
	
	$.mobile.changePage(
		"levelselector.html",
	    {
	      transition       	: 'pop',
	      reverse			: true
	    }
	);
}

//load the javascript file and the description of the level and execute the javascript file
function loadLevel ( level ) {
	resetCanvas();
	//level = 1; //for debugging
	
	//force the game & level javascript to load first
	$.when(
		    $.getScript( "js/theme4/game.js" ),
		    $.getScript( "js/theme4/level.js" ),
		    $.Deferred(function( deferred ){
		        $( deferred.resolve );
		    })
		).done(function(){
		    //place your code here, the scripts are all loaded
			var url = "js/theme4/level" + level + ".js";
			
			switch(level)
			{
			  case 1:
				  $.getScript( url, function() {
					  daniel_level1();
					  $('#leveltxt').text("LEVEL 1");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");			  
				  });
			    break;
			  case 2:
				  $.getScript( url, function() {
					  daniel_level2();
					  $('#leveltxt').text("LEVEL 2");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 3:
				  $.getScript( url, function() {
					  daniel_level3();
					  $('#leveltxt').text("LEVEL 3");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 4:
				  $.getScript( url, function() {
					  daniel_level4();
					  $('#leveltxt').text("LEVEL 4");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 5:
				  $.getScript( url, function() {
					  daniel_level5();
					  $('#leveltxt').text("LEVEL 5");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 6:
				  $.getScript( url, function() {
					  daniel_level6();
					  $('#leveltxt').text("LEVEL 6");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 7:
				  $.getScript( url, function() {
					  daniel_level7();
					  $('#leveltxt').text("LEVEL 7");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 8:
				  $.getScript( url, function() {
					  daniel_level8();
					  $('#leveltxt').text("LEVEL 8");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 9:
				  $.getScript( url, function() {
					  daniel_level9();
					  $('#leveltxt').text("LEVEL 9");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  case 10:
				  $.getScript( url, function() {
					  daniel_level10();
					  $('#leveltxt').text("LEVEL 10");
					  $('#leveldescription').text("Connect the electricity from the socket(red) to all the eggs(pink) using blocks(green)");	
				  });
			    break;
			  default:
			    alert("Error could not load level" + level);
			} 	 			
		});
}

